package cn.enine.tbcharge.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cn.enine.tbcharge.R;
import cn.enine.tbcharge.base.MyBaseActivity;
import cn.enine.tbcharge.pojo.AppUser;
import cn.enine.tbcharge.pojo.AppXmpy;
import cn.enine.tbcharge.pojo.RetMsg;
import cn.enine.tbcharge.util.HttpUrl;
import cn.enine.tbcharge.util.MyToastUtil;
import cn.enine.tbcharge.util.NetWorkUtils;
import cn.enine.tbcharge.util.RegexUtils;
import cn.enine.tbcharge.util.SharedPreferencesUtils;
import cn.enine.tbcharge.util.StringUtils;
import cn.enine.tbcharge.util.UserClient;
import cn.pedant.SweetAlert.SweetAlertDialog;

public class AppUserActivity extends MyBaseActivity {

    //标题
    TextView title, uname;
    LinearLayout back;
    Context con = AppUserActivity.this;

    private EditText name,ssdw,phone;
    AppUser obj;
    String url="";

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_app_user);
        if (NetWorkUtils.isNetworkConnected(con)) {
            getObject();
            init(obj);
        }
    }

    private void init(final AppUser obj){
        //姓名
        name = (EditText) findViewById(R.id.name);
        if (obj != null && !StringUtils.isEmpty(obj.getName())) {
            name.setText(obj.getName());
        }
        ssdw = (EditText) findViewById(R.id.ssdw);
        if (obj != null && !StringUtils.isEmpty(obj.getYljgname())) {
            ssdw.setText(obj.getYljgname());
        }
        phone = (EditText) findViewById(R.id.phone);
        if (obj != null && !StringUtils.isEmpty(obj.getPhone())) {
            phone.setText(obj.getPhone()+"");
        }
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("用户信息");
        uname.setText("保存");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phoneText = phone.getText().toString();
                if(null != phoneText && !"".equals(phoneText) && !RegexUtils.isMobileSimple(phoneText)){
                    MyToastUtil.ShowToast(con,"请填写正确手机号码");
                }else if (NetWorkUtils.isNetworkConnected(con)) {  ///网络连接
                    url = HttpUrl.EditUser;
                    RequestParams ps = new RequestParams();
                    ps.add("entity.ID", SharedPreferencesUtils.getParam(con, "username", "").toString());
                    ps.add("entity.PHONE", phoneText);
                    ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                    ps.add("type", "2");
                    UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(String content) {
                            super.onSuccess(content);
                            MyToastUtil.ShowToast(con, "成功");
                            finish();
                        }
                        @Override
                        public void onFailure(Throwable error) {
                            super.onFailure(error);
                            MyToastUtil.ShowToast(con, "失败");
                        }
                    });
                } else {

                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {

        } else {
            Log.i("requestCode====",requestCode+"");
        }
    }

    public void getObject() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        UserClient.get(HttpUrl.GetUser, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    JSONObject jsonObject = JSON.parseObject(ret.getRd());
                    obj = new AppUser();
                    obj.setName(jsonObject.getString("NAME"));
                    obj.setYljgname(jsonObject.getString("YLDWMC"));
                    obj.setPhone(jsonObject.getString("PHONE"));

                    name.setText(obj.getName());
                    ssdw.setText(obj.getYljgname());
                    phone.setText(obj.getPhone()==null?"":obj.getPhone());
                } else {
                    new SweetAlertDialog(con, SweetAlertDialog.ERROR_TYPE).setTitleText("提示")
                            .setContentText(ret.getInfo())
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    finish();
                                }
                            }).show();
                }

            }
            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onFailure(Throwable error) {
                super.onFailure(error);
                obj = new AppUser();
                init(obj);
            }
        });
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }
}
